package com.nju.edu.erp.service.Impl;


import com.nju.edu.erp.dao.PaySheetDao;
import com.nju.edu.erp.enums.sheetState.PaySheetState;
import com.nju.edu.erp.enums.sheetState.ReceiptSheetState;
import com.nju.edu.erp.model.po.AccountPO;
import com.nju.edu.erp.model.po.CustomerPO;
import com.nju.edu.erp.model.po.PaySheetPO;
import com.nju.edu.erp.model.po.ReceiptSheetPO;
import com.nju.edu.erp.model.vo.UserVO;
import com.nju.edu.erp.model.vo.pay.PaySheetVO;
import com.nju.edu.erp.model.vo.receipt.ReceiptSheetVO;
import com.nju.edu.erp.service.finance.AccountService;
import com.nju.edu.erp.service.customer.CustomerService;
import com.nju.edu.erp.service.finance.FinancePayService;
import com.nju.edu.erp.service.customer.CustomerService;
import com.nju.edu.erp.service.finance.AccountService;
import com.nju.edu.erp.utils.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class  FinancePayServiceImp implements FinancePayService {
    PaySheetDao paySheetDao;
    AccountService accountService;
    CustomerService customerService;

    @Autowired
    public FinancePayServiceImp(PaySheetDao paySheetDao, CustomerService customerService,AccountService accountService) {
        this.paySheetDao = paySheetDao;
        this.customerService = customerService;
        this.accountService= accountService;
    }

    @Override
    @Transactional
    public void makePaySheet(UserVO userVO, PaySheetVO paySheetVO) {
        PaySheetPO paySheetPO = new PaySheetPO();
        BeanUtils.copyProperties(paySheetVO, paySheetPO);

        paySheetPO.setOperator(userVO.getName());
        PaySheetPO latest = paySheetDao.getLatest();
        String id = IdGenerator.generateSheetId(latest == null ? null : latest.getId(), "XJFYD");
        paySheetPO.setId(id);
        paySheetPO.setState(PaySheetState.PENDING);
        paySheetDao.save(paySheetPO);
    }

    ;

    @Override
    public List<PaySheetVO> getPaySheetByState(PaySheetState state) {
        List<PaySheetVO> res = new ArrayList<>();
        List<PaySheetPO> all;
        if (state == null) {
            all = paySheetDao.findAll();
        } else {
            all = paySheetDao.findAllByState(state);
        }
        for (PaySheetPO po : all) {
            PaySheetVO vo = new PaySheetVO();
            BeanUtils.copyProperties(po, vo);
            res.add(vo);
        }
        return res;
    }

    ;

    public void approval(String paySheetId, PaySheetState state) {
            // TODO 审批完成, 修改一系列状态
            // 更新客户表(更新应付字段)
            // 更新应付 payable
            paySheetDao.updateState(paySheetId, state);
            PaySheetPO paySheet = paySheetDao.findOneById(paySheetId);
            CustomerPO customerPO = customerService.findCustomerByName(paySheet.getCustomer());
            customerPO.setPayable(customerPO.getPayable().subtract(paySheet.getTotalAmount()));
            customerService.updateCustomer(customerPO);

            AccountPO accountPO = accountService.findAccountByName(paySheet.getAccount());
            accountPO.setMoney(accountPO.getMoney().add(paySheet.getTotalAmount()));

    }
}
